// 退款原因类型
var resendReasonTypeTable;

$(document).ready(function() {
    setPageElementStatus("resendReasonTypeList");

    $.validator.addMethod("checkLetter", function(value, element) {
        var reg = /^[0-9a-zA-Z\u4E00-\u9FA5\r\n\s/.]*$/;
        if (!reg.test(value)) {
            return false;
        }

        return true;
    }, "请输入数字，大小写字母，汉字");

    $("#newForm").validate({
        debug:true,
        errorPlacement: function(error, element)
        {
            error.insertAfter(element);
        }
    });

    $("#editForm").validate({
        debug:true,
        errorPlacement: function(error, element)
        {
            error.insertAfter(element);
        }
    });

    // 退款原因类型list
    resendReasonTypeTable = $('#tblresendReason').DataTable({
        "processing": true,
        "serverSide": true,
        "lengthMenu" : [ [ 10, 25, 50, 100, 500, 1000 ], [ 10, 25, 50, 100, 500, 1000] ],
        "dom": '<"bottom"rtflp>',
        "searching": false,
        "pagingType": "full_numbers",
        "deferRender": true,
        "ajax": {
            "url": "/resendReasonType/getResendReasonTypeList.action",
            "data": function ( d ) {
                d.quickSearch = encodeURI($('#quickSearch').val());
            }
        },
        "tableTools": {
            "sRowSelect": "bootstrap"
        },
        "columns": [
            {"data": "resendReasonCode", "searchable": false},
            {"data": "resendReasonName", "searchable": false},
            {"data": "remark", "searchable": false},
            {"data": "operation", "searchable": false, "orderable": false}
        ],
        "columnDefs": [
            {"className": "truncate-text rdfundReasonType-column-detail", "targets":[0]},
            {"className": "truncate-text rdfundReasonType-column-detail", "targets":[1]},
            {"className": "truncate-text rdfundReasonType-column-detail", "targets":[2]},
            {"targets":[3]}
        ],
        "order": [[ 0, "asc" ]]
    });

    // 文字过长截断显示悬浮框
    $("#tblresendReason").tooltip({
        selector: ".rdfundReasonType-column-detail",
        container: "body",
        title: function() {
            if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                return $(this).text();
            }
        }
    });

    // 快速搜索
    $("#btnQuickSearch").click(function () {
        resendReasonTypeTable.draw();
    });

    // 搜索框的回车事件
    $('#quickSearch').keydown(function(e){
        if(e.keyCode==13){
            $("#btnQuickSearch").click();
        }
    });

    $('#frmNewresendReasonType').on('hidden.bs.modal', function () {
        $("#newForm")[0].reset();
    });

    $('#frmNewresendReasonType').on('hide.bs.modal', function () {
        window.top.window.returnCustomModalDialog();
        $("#newForm").validate().clear();
    });

    $('#frmModifyresendReasonType').on('hidden.bs.modal', function () {
        $("#editForm")[0].reset();
    });

    $('#frmModifyresendReasonType').on('hide.bs.modal', function () {
        window.top.window.returnCustomModalDialog();
        $("#editForm").validate().clear();
    })

    // $('#frmSelPlatform').on('hide.bs.modal', function () {
    //     window.top.window.returnCustomModalDialog();
    // });
    //
    // $('#frmEditPlatform').on('hidden.bs.modal', function () {
    //     $("#editPlatformForm")[0].reset();
    // });
    //
    // $('#frmEditPlatform').on('hide.bs.modal', function () {
    //     window.top.window.returnCustomModalDialog();
    //     window.top.window.borrowCustomModalDialog($("#frmSelPlatform"));
    // });

});

//弹出新增退款原因类型对话框
$("#newresendReasonTypeHref").click(function () {
    var editDialog = window.top.window.borrowCustomModalDialog($("#frmNewresendReasonType"));
    editDialog.modal({show:true, backdrop:'static'});
});


// 保存新增退款原因类型
$("#btnSave").click(function () {
    window.top.window.returnCustomModalDialog();

    if(!$("#newForm").valid()) {
        window.top.window.borrowCustomModalDialog($("#frmNewresendReasonType"));
        return false;
    }

    var resendReasonCode = $('#resendReasonCode').val();
    var resendReasonName = $('#resendReasonName').val();
    var remark = $('#remark').val();
    var data = {
        "resendReasonCode": resendReasonCode,
        "resendReasonName": resendReasonName,
        "remark": remark
    }



    var newForm=window.top.window.borrowCustomModalDialog($("#frmNewresendReasonType"));

    $.ajaxSettings['contentType'] = "application/x-www-form-urlencoded; charset=utf-8";
    $.ajax({
        type: "POST",
        url: "/resendReasonType/insertresendReasonType.action",
        data: JSON.stringify(data),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (json) {
            if (json.result=="success") {
                newForm.modal('hide');
                $('#resendReasonCode').val('');
                $('#resendReasonName').val('');
                $('#remark').val('');
                window.top.window.showScoMessage('ok', '添加成功');
                resendReasonTypeTable.draw();
            } else {
                window.top.window.showModalAlert(json.msg);
            }
        }
    });
});

// 弹出修改原因类型对话框
function operationEdit(btnEdit) {
    var resendReasonId = $(btnEdit).attr("resendReasonId");
    $.ajax({
        type: "POST",
        url: "/resendReasonType/getresendReasonTypeInfo/" + resendReasonId + ".action",
        data: null,
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                $('#resendReasonId_m').val(resendReasonId);
                $('#resendReasonCode_m').val(data.data.resendReasonCode);
                $('#resendReasonName_m').val(data.data.resendReasonName);
                $('#remark_m').val(data.data.remark);
                var editDialog = window.top.window.borrowCustomModalDialog($("#frmModifyresendReasonType"));
                editDialog.modal({show:true, backdrop:'static'});
            } else{
                window.top.window.showModalAlert(data.msg);
            }
        }
    });

    // $('#frmModifyresendReasonType').modal('show');
}

// 保存修改退款原因类型
$("#btnSave_m").click(function () {
    window.top.window.returnCustomModalDialog();

    if(!$("#editForm").valid()) {
        window.top.window.borrowCustomModalDialog($("#frmModifyresendReasonType"));
        return false;
    }

    var resendReasonId = $('#resendReasonId_m').val();
    var resendReasonCode = $('#resendReasonCode_m').val();
    var resendReasonName = $('#resendReasonName_m').val();
    var remark = $('#remark_m').val();
    var data = {
        "resendReasonId": resendReasonId,
        "resendReasonCode": resendReasonCode,
        "resendReasonName": resendReasonName,
        "remark": remark
    }

    var edit=window.top.window.borrowCustomModalDialog($("#frmModifyresendReasonType"));

    $.ajaxSettings['contentType'] = "application/x-www-form-urlencoded; charset=utf-8";

    $.ajax({
        type: "POST",
        url: "/resendReasonType/updateresendReasonType.action",
        data: JSON.stringify(data),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (json) {
            if (json.result == "success") {
                edit.modal('hide');
                $('#resendReasonCode_m').val('');
                $('#resendReasonName_m').val('');
                $('#remark_m').val('');
                window.top.window.showScoMessage('ok', '保存成功');
                resendReasonTypeTable.draw();
            } else {
                window.top.window.showModalAlert(json.msg);
            }
        }
    });
});

// 删除原因类型
function operationDelete(btnDelete) {
    var resendReasonId = $(btnDelete).attr("resendReasonId");
    $('#resend_reason_id_delete').val(resendReasonId);
    window.top.window.showModalConfirm( "是否确认删除？", doDelete);
}

// 删除原因类型
function doDelete() {
    var resendReasonId = $('#resend_reason_id_delete').val();

    $.ajax({
        type: "POST",
        url: "/resendReasonType/deleteresendReasonType/" + resendReasonId + ".action",
        data: null,
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (json) {
            if (json.result == "success") {
                window.top.window.showScoMessage('ok', '删除成功');
                resendReasonTypeTable.draw();
            } else {
                window.top.window.showModalAlert(json.msg);
            }
        }
    });
}
// $('#frmSelPlatform').on('shown.bs.modal', function (e) {
//     //表头重新绘制
//     resendReasonRelationTable.columns.adjust().draw();;
// })

// 弹出平台退款原因list关联对话框
var resendReasonRelationFlg = false;
// function operationSelect(btnSel) {
//     if (resendReasonRelationFlg) {
//         resendReasonRelationTable.destroy();
//     }
//
//     $('#resend_reason_id').val($(btnSel).attr("resendReasonId"));
//
//     resendReasonRelationTable = $('#tblresendReasonRelation').DataTable({
//         "paging": false,
//         "processing": true,
//         "serverSide": true,
//         "dom": '<"bottom"rtflp>',
//         "searching": false,
//         "scrollX" : "100%",
//         "fixedHeader": true,
// //	    "scrollXInner" : "110%",
//         "deferRender": true,
//         "ajax": {
//             "url": "/resendReasonType/getresendReasonRelationList.action",
//             "dataFilter": function(data) {
//                 var array = jQuery.parseJSON(data);
//                 return JSON.stringify({
//                     data: array,
//                     recordsTotal: array.length,
//                     recordsFiltered: array.length
//                 });
//             },
//             "data": function ( d ) {
//                 d.resendReasonId = $('#resend_reason_id').val();
//             }
//         },
//         "fnDrawCallback": function (oSettings) {
//             window.top.window.$("#thresendReasonRelation").removeClass("sorting_asc");
//         },
//         "tableTools": {
//             "sRowSelect": "bootstrap"
//         },
//         "columns": [
//             {"data": "ecresendReason", "searchable": false, "orderable": false},
//             {"data": "platform",
//                 "render": function ( data, type, row, meta ) {
//                     if (row.platform == "tb") {
//                         return "淘宝";
//                     }else if(row.platform == "xvp"){
//                         return "小V铺";
//                     }else{
//                         return "其它";
//                     }
//                 },
//                 "searchable": false, "orderable": false},
//             {"data": "goodsStatus",
//                 "render": function ( data, type, row, meta ) {
//                     if (row.goodsStatus == "received") {
//                         return "已收到货";
//                     } else {
//                         return "未收到货";
//                     }
//                 },
//                 "searchable": false, "orderable": false},
//             {"data": "goodsReturn",
//                 "render": function ( data, type, row, meta ) {
//                     if (row.goodsReturn == "y") {
//                         return "退货";
//                     } else {
//                         return "不退货";
//                     }
//                 },
//                 "searchable": false, "orderable": false},
//             {"mRender": function(data, type, row) {
//                 return '<button data-relation-id="' + row.relationId + '" type="button" class="clickClass btn btn-danger btn-xs"><i class="fa fa-trash-o"></i>&nbsp;删除</button>';
//             },
//                 "searchable": false, "orderable": false}
//         ]
//     });
//
//     resendReasonRelationFlg = true;
//     var selDialog = window.top.window.borrowCustomModalDialog($("#frmSelPlatform"));
//     selDialog.modal({show:true, backdrop:'static'});
//     $('#frmSelPlatform').modal('show');
// }

// // 删除平台退款原因关联
// $('#tblresendReasonRelation').on("click", ".clickClass", function() {
//     var relationId = $(this).data("relation-id");
//     $('#relation_id_delete').val(relationId);
//     window.top.window.showModalConfirm( "是否确认删除？", doRelationDelete);
// });
//
// // 删除平台退款原因关联
// function doRelationDelete() {
//     var relationId = $('#relation_id_delete').val();
//
//     $.ajax({
//         type: "POST",
//         url: "/resendReasonType/deleteresendReasonRelation/" + relationId + ".action",
//         data: null,
//         contentType: "application/json;charset=utf-8",
//         dataType: "json",
//         success: function (json) {
//             if (json.success) {
//                 window.top.window.showScoMessage('ok', '删除成功');
//                 resendReasonRelationTable.draw();
//             } else {
//                 window.top.window.showModalAlert(json.msg);
//             }
//         }
//     });
// }

